import mx.utils.Delegate;
import com.dmotiko.general.*
import com.dmotiko.sathya.*

class PrensaContainer
extends BaseClip {
	
	private static var X_SPACE:Number = 4;
	private static var Y_SPACE:Number = 4;
	private static var THUMBS_NUMBER:Number = 6;
	
	private var nThumb:Number;
	private var aThumbs:Array;
	private var thumbActive;
	
	function PrensaContainer() {
		super();
		aThumbs = new Array();
		nThumb = 0;
		
	}
		
	public function setData(data){
		super.setData(data);
		
		for (var i:Number = 0; i < data.length; i++) {
			
			var clip = this.attachMovie("prensaThumb", "mcPrensaThumb"+i, this.getNextHighestDepth());
			clip.addEventListener( MovieLoaderClip.CLIP_LOADED, Delegate.create(this, clipLoaded) );
			clip.addEventListener( "onRelease", Delegate.create(this, clipActive) );
			clip.setData( data[i] );
			aThumbs.push(clip);
			
		}
	}
	
	public function clipLoaded(evnt){
		nThumb++;
		if(nThumb == data.length-1 || data.length == 1) layoutThumbs();
	}
	
	public function clipActive(evnt){
		thumbActive = evnt.target;
		dispatchEvent( { target: this, type: BaseClipEvent.CHANGED, data: evnt.data } );
	}
		
	public function getThumb(nOffset:Number){
		for (var i:Number = 0; i < aThumbs.length; i++) {
			if( aThumbs[i] == thumbActive){
				if(aThumbs[i+nOffset]) return aThumbs[i+nOffset];
				else if(nOffset > 0) return aThumbs[0];
				else if(nOffset < 0) return aThumbs[aThumbs.length-1];
			}
		}
	}
	
	private function layoutThumbs(){
		var nX:Number = 0;
		var nY:Number = 0;
		for (var i:Number = 0; i < aThumbs.length; i++) {
			var thumb = aThumbs[i];
			if( i > 0 && i%THUMBS_NUMBER == 0 ){
				nX = 0;
				nY += thumb._height + Y_SPACE;
			}
			thumb._x = nX;
			thumb._y = nY;
			nX += thumb._width + X_SPACE;
						
			thumb.show();
		}
	}
	
}